using Microsoft.EntityFrameworkCore;
using YoYoMoocExampleApp.Models;

public static class SeedData{
    public static IApplicationBuilder UseDataInitalizer(this IApplicationBuilder builder){
        using(var scope=builder.ApplicationServices.CreateScope()){
            var dbContext=scope.ServiceProvider.GetService<ProductDbContext>();
            System.Console.WriteLine("开始执行迁移数据库...");
            dbContext.Database.Migrate();
            System.Console.WriteLine("数据库迁移完成....");
            if(!dbContext.Products.Any()){
                System.Console.WriteLine("开始创建种子数据中....");
                dbContext.Products.AddRange(
                    new Product ("空调","家用电器",2750),
                    new Product ("电视机","家用电器",2448.95m),
                    new Product ("洗衣机","家用电器",1440.50m),
                    new Product ("抽烟机","家用电器",3454.95m),
                    new Product ("冰箱","家用电器",9500),
                    new Product ("猪肉","食品",36),
                    new Product ("牛肉","食品",49.95m),
                    new Product ("鸡肉","食品",22),
                    new Product ("鸭肉","食品",18)

                );
                dbContext.SaveChanges();
            }else{
                System.Console.WriteLine("无需创建种子数据");
            }
        }
        return builder;
    }
}